#include <stdlib.h>
#include "cinta.h"


struct celda
{
	Tuple elem;
	aList tl;
};

struct sCinta
{
	struct celda cd;
	aList ppd;
	Bool arr;
};

aList
aList_vacia(void)
{
	return NULL;
}

aList
aList_destroy(aList l)
{
	aList aux;
	while (l!=NULL)
	{
		aux = l;
		l->elem = tuple_destroy(l->elem);
		l = aux->tl;
		free(aux);
	}
return l;
}

Cinta
cinta_create(aList l)
{
	Cinta c;
	c = (Cinta) malloc (sizeof(struct sCinta));
	if (c!=NULL)
	{
		(c->cd).tl = l;
		c->arr = FALSE;
		c->ppd = NULL;
	}
	else
		exit(EXIT_FAILURE);

return c;
}

void
cinta_arr(Cinta c)
{
	c->ppd = &(c->cd);
	c->arr = TRUE;
}

Bool
cinta_fin(Cinta c)
{
	Bool b;
	if (c->arr)
		b = (c->ppd->tl==NULL);
return b;
}

void
cinta_av(Cinta c)
{
	if (!(cinta_fin(c)) && c->arr)
		c->ppd = c->ppd->tl;
}

Tuple
cinta_elec(Cinta c)
{
	Tuple t;
	if (!(cinta_fin(c)) && c->arr)
		t = tuple_clone(c->ppd->tl->elem);
return t;
}

void
cinta_ins(Cinta c, Tuple t)
{
	aList aux;
	Tuple t2;
  	if (c->arr)
  	{
    	aux = (c -> ppd)->tl;
    	(c->ppd)->tl = (aList)malloc(sizeof(struct celda));
    	if (((c->ppd)->tl) != NULL)
    	{
			t2 = tuple_clone(t);
      		((c->ppd)->tl)-> elem = t2;
      		((c->ppd)->tl) -> tl = aux;		
    	}
  	}
}

void
cinta_del(Cinta c)
{
	aList aux;
  	if (!(cinta_fin(c)) && (c->arr))
  	{		
    	aux = (c->ppd)->tl;
    	(c->ppd)->tl = (aux->tl);
    	aux->elem = tuple_destroy(aux->elem);
    	free(aux);
  	}
}

aList
cinta_destroy(Cinta c)
{
	aList l;
	if (c!=NULL)
	{
		l = (c->cd).tl;		
		free(c);
		c = NULL;
	}
return l;
}

